<template>
  <div class="app-page-padding">
    <!-- 标题 -->
    <!-- <div class="app-header">
      <div class="app-title">
        <i :class="$route.meta.icon"></i>  {{ $route.meta.title }}
      </div>
      <div class="app-tools">
        <el-button icon="fa fa-filter" size="small" @click="showSearch = !showSearch">
          {{ showSearch?'隐藏':'查询' }}
        </el-button>
        <el-button icon="fa fa-plus" size="small" @click="goRouterView('/add')">
          添加
        </el-button>
      </div>
    </div> -->
    <!-- 内容 -->

    <!-- 搜索 -->

    <el-form
      ref="form"
      :model="searchForm"
      size="small"
      inline
      class="app-search-form"
      label-width="80px"
    >
      <CollapseSearch :visible.sync="showSearch">
        <el-form-item style="float:right; margin-right:40px;">
          <el-button type="primary" @click="query(searchForm)">
            查询
          </el-button>
          <el-button @click="resetForm">
            重置
          </el-button>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="searchForm.name01" placeholder="请选择" clearable style="width:200px">
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
      </CollapseSearch>
    </el-form>
    <div v-if="showSearch"></div>
    <!-- 表格 -->
    <div v-auto-height class="app-table-box">
      <el-table v-loading="tableLoading" :data="tableData" border height="100%" style="width: 100%">
        <el-table-column type="index" width="70" align="center" label="序号" />
        <el-table-column prop="field01" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field02" align="center" label="类型" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field03" align="center" label="计划开始时间" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.field03 }}
          </template>
        </el-table-column>
        <el-table-column prop="field04" align="center" label="当前阶段" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" class="primary-color" size="medium" @click="goRouterView(`/edit?uuid=${scope.row.uuid}`)">
              编辑
            </el-button>
            <el-button type="text" class="danger-color" size="medium" @click="delRow(scope.row.uuid)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="pagination.current"
      :page-sizes="pagination.sizes"
      :page-size="pagination.size"
      :total="pagination.total"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChange"
      @current-change="pageChange"
    >
    </el-pagination>
  </div>
</template>
<script>
import { remove, getData } from '@/mock/index'

// 搜索分页表
import tableSearchPaginationMixin from '@/mixins/tableSearchPagination.js'
export default {
  name: 'TablelistTableSearchPagination',
  mixins: [tableSearchPaginationMixin],
  data () {
    return {
    }
  },
  mounted () {
    this.getTableData()
  },
  methods: {
    // 获取列表
    async getTableData (form = this.searchFormTemp) {
      console.log(form)
      // 模拟表格数据
      const tableData = {
        current: 1,
        pages: 1,
        records: [
          {
            field01: '项目A',
            field02: '',
            field03: '2019-3-12',
            field04: '论证阶段',
            field05: '210000',
            field06: '',
            uuid: 'uuid'
          }
        ],
        size: 20,
        total: 1
      }
      const { current, size } = this.pagination
      this.tableLoading = true
      const res = await getData({ current, size, ...form }, tableData)
      this.tableLoading = false
      if (res.code === '0000') {
        const { current, size, total, records } = res.data
        // console.log(res)
        this.pagination = Object.assign({}, this.pagination, { current, size, total })
        this.tableData = records
      }
    },
    // 删除
    delRow (uuid) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ userUuid: uuid })
        if (res.code === '0000') {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 删除最后一个时处理
          if (this.tableData.length === 1 && this.pagination.current > 1) {
            this.pagination.current = this.pagination.current - 1
          }
          // 更新表格
          this.getTableData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
